package com.example.forum.controller;


import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardService;
import com.example.forum.services.impl.BoardServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(tags = "板块接口")
@RestController
@RequestMapping("/board")
public class BoardController {
    @Autowired
    private IBoardService boardService;

    //  从配置中读取默认的板块数量
    @Value("${forum.index.board-num:9}")
    private Integer boardNum;

    @ApiOperation(("主页中显示的板块"))
    @GetMapping("/toplist")
    public AppResult topList() {
        List<Board> list = boardService.selectByNum(boardNum);
        return AppResult.success(list);
    }

    @ApiOperation("获取指定id板块的信息")
    @GetMapping("/getbyid")
    public AppResult getById(@ApiParam("板块id") @RequestParam("id") @NonNull Long id) {
        Board board = boardService.selectById(id);

        if(board == null ||board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
            return AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }

        return AppResult.success(board);
    }
}
